package com.aaa.controller;

import com.aaa.unilt.UploadUnilt;
import com.aaa.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/9/26
 */
@CrossOrigin
@Controller
@RequestMapping("upload")
public class Test02Interceptor {
    @RequestMapping("/upload02")
    @ResponseBody
    public Result upload02(MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println(file);
        //获取上传路径
        String path = request.getSession().getServletContext().getRealPath("upload");
        File file1 = new File(path);
        //判断文件是否存在
        if(!file1.exists()){
            file1.mkdirs();//创建文件夹
        }
        //把上面的file转移到target目录下
        System.out.println(file.getOriginalFilename());
        String filename = UUID.randomUUID() + file.getOriginalFilename();
        File file2 = new File(path + "/"+filename);
        file.transferTo(file2);

        return new Result(200,"上传成功",path+"/"+filename);
    }

    @RequestMapping("/upload03")
    @ResponseBody
    public Result upload03(MultipartFile file, HttpServletRequest request) throws Exception {
        String path = UploadUnilt.ossUpload(file);
        System.out.println("~~~~~~~~"+path);
        return new Result(200,"上传成功",path);
    }
}
